package com.atguigu.crowd.funding.handler;

import com.atguigu.crowd.funding.entity.Admin;
import com.atguigu.crowd.funding.entity.ResultEntity;
import com.atguigu.crowd.funding.service.api.AdminService;
import com.atguigu.crowd.funding.util.CrowdFundingConstant;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author hftang
 * @date 2019-10-23 10:56
 * @desc
 */

@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    @RequestMapping("admin/update")
    public String updateAdmin(Admin admin, @RequestParam("pageNum") Integer pageNum) {

        this.adminService.updateAdmin(admin);
        return "redirect:/admin/query/for/search.html?pageNum=" + pageNum;
    }

    @RequestMapping("admin/to/edit/page")
    public String editAdmin(@RequestParam("adminId") Integer adminId, Model model) {

        Admin admin = this.adminService.findAdminById(adminId);

        model.addAttribute("admin", admin);

        return "admin-edit";

    }


    //添加admin]
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin) {

        this.adminService.saveAdmin(admin);

        return "redirect:/admin/query/for/search.html";

    }

    //批量删除
    @ResponseBody
    @RequestMapping("/admin/batch/remove")
    public ResultEntity<String> batchDel(@RequestBody List<Integer> ids) {

        try {
            this.adminService.batchRemove(ids);
            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(null, e.getMessage());
        }


    }

    //分页查询
    @RequestMapping("/admin/query/for/search")
    public String queryKeywordSearch(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, Model model) {

        PageInfo<Admin> pageInfo = this.adminService.queryForKeywordSearch(keyword, pageNum, pageSize);
        List<Admin> list = pageInfo.getList();

        long total = pageInfo.getTotal();

        model.addAttribute(CrowdFundingConstant.PAGE_INFO, pageInfo);

        return "admin-page";

    }

    @RequestMapping("/admin/get/all")
    public String getAll(Model model) {

        List<Admin> all = this.adminService.getAll();

        model.addAttribute("list", all);

        return "admin-target";

    }

    //admin登录
    @RequestMapping("/admin/do/login")
    public String doLogin(@RequestParam("loginAcct") String loginAcct, @RequestParam("userPswd") String userPswd, HttpSession httpSession, Model model) {

        if (StringUtils.isBlank(loginAcct) || StringUtils.isBlank(userPswd)) {
            model.addAttribute("MESSAGE", CrowdFundingConstant.UserNameOrPwdError);
            return "admin-login";
        }


        Admin admin = this.adminService.doLogin(loginAcct, userPswd);
        if (admin == null) {
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
        //存入session
        httpSession.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, admin);

        return "redirect:/admin/to/main/page.html";
    }

    //退出登录
    @RequestMapping("/admin/logout")
    public String loginOut(HttpSession httpSession) {

        httpSession.invalidate();
        System.out.println("退出登录");

        return "redirect:/index.html";

    }


}
